package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.bean.PayWayBean;

import java.util.List;

/**
 * Created by XinEr on 2019/7/31.
 * Describe:
 */
public class PayWayAdapter extends RecyclerView.Adapter<PayWayAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<PayWayBean.PaymentListBean> paymentList;
    public int mSelectedPos = -1;//保存当前选中的position

    public void setmSelectedPos(int mSelectedPos) {
        this.mSelectedPos = mSelectedPos;
    }

    private Double yueMoney;
    private String orderPrice;
    private int orderType = 0;


    public int balancePayId=-1;

    public int getBalancePayId() {
        return balancePayId;
    }

    public void setBalancePayId(int balancePayId) {
        this.balancePayId = balancePayId;
    }

    public int getOrderType() {
        return orderType;
    }

    public void setOrderType(int orderType) {
        this.orderType = orderType;
    }

    public String getOrderPrice() {
        return orderPrice;
    }

    public void setOrderPrice(String orderPrice) {
        this.orderPrice = orderPrice;
    }

    public Double getYueMoney() {
        return yueMoney;
    }

    public void setYueMoney(Double yueMoney) {
        this.yueMoney = yueMoney;
    }

    public PayWayAdapter(Context context, List<PayWayBean.PaymentListBean> paymentList) {
        this.context = context;
        this.paymentList = paymentList;
    }

    @NonNull
    @Override
    public PayWayAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.pay_way_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull PayWayAdapter.ViewHolder viewHolder, final int i) {
        PayWayBean.PaymentListBean paymentListBean = paymentList.get(i);


        if (mSelectedPos == i) {
            viewHolder.chat_btn.setSelected(true);
        } else if (mSelectedPos != -1) {
            viewHolder.chat_btn.setSelected(false);
        }
        //培训没有到店支付和余额支付
        if (orderType==5){
            if (paymentListBean.getPayId() == 6||paymentListBean.getPayId() == 5){
                viewHolder.rl_select.setVisibility(View.GONE);
            }

        }
        //金额为0  只要余额支付
        if (Double.parseDouble(getOrderPrice())<=0){
                //orderType==4才有余额支付
                if (orderType==4){
                    if (paymentListBean.getPayName().equals("余额支付")){
                        viewHolder.iv_chat.setImageResource(R.mipmap.ic_yue);
                        viewHolder.tv_payway_title.setText(paymentListBean.getPayName() + "(剩余:  ¥" + getYueMoney() + ")");
                        setBalancePayId(5);
                        viewHolder.chat_btn.setSelected(true);
                        viewHolder.rl_select.setVisibility(View.VISIBLE);
                    }else {
                        viewHolder.rl_select.setVisibility(View.GONE);
                    }

                }else {
                    viewHolder.rl_select.setVisibility(View.GONE);
                }

        }else {
            //payId  2  微信支付   3     支付宝     4    银行汇款/转账     6   到店支付     5   余额支付
            if (paymentListBean.getPayId() == 2) {
                viewHolder.iv_chat.setImageResource(R.mipmap.ic_chat_pay);
                viewHolder.tv_payway_title.setText(paymentListBean.getPayName());
            } else if (paymentListBean.getPayId() == 3) {
                viewHolder.iv_chat.setImageResource(R.mipmap.ic_alipay);
                viewHolder.tv_payway_title.setText(paymentListBean.getPayName());
            } else if (paymentListBean.getPayId() == 4) {
                viewHolder.iv_chat.setImageResource(R.mipmap.ic_zhuanzhang);
                viewHolder.tv_payway_title.setText(paymentListBean.getPayName());
            } else if (paymentListBean.getPayId() == 5) {
                //orderType==4才有余额支付
                if (orderType==4){
                    viewHolder.iv_chat.setImageResource(R.mipmap.ic_yue);
                    viewHolder.tv_payway_title.setText(paymentListBean.getPayName() + "(剩余:  ¥" + getYueMoney() + ")");
                    viewHolder.rl_select.setVisibility(View.VISIBLE);
                }else {
                    viewHolder.rl_select.setVisibility(View.GONE);
                }

            } else if (paymentListBean.getPayId() == 6) {
                viewHolder.iv_chat.setImageResource(R.mipmap.ic_shop_pay);
                viewHolder.tv_payway_title.setText(paymentListBean.getPayName());
            }else if (paymentListBean.getPayId()==7){
                //融币支付
                viewHolder.rl_select.setVisibility(View.GONE);
            }
        }

//        viewHolder.rl_select.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onUpTimeClick(View v) {
//
//
//
//            }
//        });
        viewHolder
                .setIsRecyclable(false);
        viewHolder.itemView.setTag(i);
    }



    @Override
    public int getItemCount() {
        return paymentList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_chat;
        public ImageView chat_btn;
        private TextView tv_payway_title;
        private RelativeLayout rl_select;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_chat = itemView.findViewById(R.id.iv_chat);
            chat_btn = itemView.findViewById(R.id.chat_btn);
            tv_payway_title = itemView.findViewById(R.id.tv_payway_title);
            rl_select = itemView.findViewById(R.id.rl_select);
        }
    }
    public interface OnClickItem{
        void OnClickListion(View view,int postion);
    }
    private PayWayAdapter.OnClickItem onClickItem;
    @Override
    public void onClick(View v) {
        if (onClickItem!=null){
            onClickItem.OnClickListion(v,(int)v.getTag());
        }
    }
    public void setOnClickItem(PayWayAdapter.OnClickItem onClickItem){
        this.onClickItem=onClickItem;
    }
}
